package com.nike.amoeba.adapter

import android.view.View
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.appcompat.widget.AppCompatSpinner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.flyco.roundview.RoundTextView
import com.nike.amoeba.R
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.ext.setAdapterAnimation
import com.nike.amoeba.util.SettingUtil
import com.nike.amoeba.weight.MYListView
import me.hgj.jetpackmvvm.base.appContext
import org.json.JSONArray


/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class StockManageListAdapter(data: ArrayList<DataBean>) :
    BaseQuickAdapter<DataBean, BaseViewHolder>(
        R.layout.stock_manage_list, data
    ){

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: DataBean) {
        data[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title, context.getString(R.string.number) + "：" + AssetNo)
            if (Info?.size != 0){
                var sb = StringBuffer()
                Info?.forEachIndexed { index, it ->
                    sb.append("${it.allNum}/${it.NeedNum}  (" +it.MUnitName + ")")
                    if (index < Info?.size?.minus(1) ?: 0) {
                        sb.append("、")
                    }
                }
                viewHolder.setText(R.id.tv_text2,context.getString(R.string.scanned) + "：")
                viewHolder.setText(R.id.tv_text1,sb.toString())
            }
            viewHolder.getView<AppCompatCheckBox>(R.id.cb_).run {
                isChecked = if (Type == 0) false else true
            }
            val myListView = viewHolder.getView<MYListView>(R.id.listview)
            if (!content.isNullOrEmpty()) {
                myListView.visibility = View.VISIBLE
                val listBean = ArrayList<DataBean>()
                val jsonArray = JSONArray(content)
                val limit = minOf(jsonArray.length(), 6)
                for (i in 0 until limit) {
                    val opt = jsonArray.optJSONObject(i)
                    val bean = DataBean().apply {
                        Title = opt.optString("Title")
                        content = opt.getString("content")
                    }
                    listBean.add(bean)
                }
                myListView.run {
                    setClickable(false)
                    setPressed(false)
                    setEnabled(false)
                    adapter = StockTextAdapter(
                        context,
                        listBean
                    )
                }
            } else {
                myListView.visibility = View.GONE
            }

            viewHolder.getView<RoundTextView>(R.id.tv_scanned).setOnClickListener {
                mItemClickListener?.onScannedClick()
            }

            viewHolder.getView<RoundTextView>(R.id.tv_detailed).setOnClickListener {
                mItemClickListener?.onDetailedClick(content, AssetNo)
            }
        }
    }


    var mItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        fun onScannedClick()
        fun onDetailedClick(content: String?, AssetNo: String?)
    }

}